﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BusinessObjects;

namespace DataAccess.SqlServer
{
    public class SqlServerThamChieuDuLieu : IThamChieuDuLieu
    {
        public ThamChieuDuLieu GetThamChieuDuLieu(int giaTri, string kieuDuLieu)
        {
            string sql = @" Select a.Oid, a.Cha, a.Con
                            From ThamChieuDuLieu a inner join KieuDuLieu b on a.Cha = b.Oid
                            Where a.GiaTri = @GiaTri and b.TenKieuDuLieu = @TenKieuDuLieu";
            SqlParameter[] param = {
                                       new SqlParameter("@GiaTri", giaTri),
                                       new SqlParameter("@TenKieuDuLieu", kieuDuLieu)
                                   };
            DataRow row = DataProvider.GetDataRow(sql, param);

            ThamChieuDuLieu thamChieuDuLieu = new ThamChieuDuLieu();
            thamChieuDuLieu.Oid = int.Parse(row["Oid"].ToString());
            thamChieuDuLieu.GiaTri = giaTri;
            int id;
            if (int.TryParse((row["Cha"].ToString()), out id))
            {
                SqlServerKieuDuLieu sqlServerKieuDuLieu = new SqlServerKieuDuLieu();
                thamChieuDuLieu.Cha = sqlServerKieuDuLieu.GetKieuDuLieu(id);
            }
            if (int.TryParse((row["Con"].ToString()), out id))
            {
                SqlServerKieuDuLieu sqlServerKieuDuLieu = new SqlServerKieuDuLieu();
                thamChieuDuLieu.Con = sqlServerKieuDuLieu.GetKieuDuLieu(id);
            }
            return thamChieuDuLieu;
        }

        public int Insert(ThamChieuDuLieu obj)
        {
            string sql = @" Insert into ThamChieuDuLieu(Cha, Con, GiaTri)
                            Values(@Cha, @Con, @GiaTri)";

            SqlParameter[] param = {
                                       new SqlParameter("@Cha", obj.Cha.Oid),
                                       new SqlParameter("@Con", obj.Con.Oid),
                                       new SqlParameter("@GiaTri", obj.GiaTri)
                                   };
            return DataProvider.Insert(sql, false, param);
        }

        public int Update(ThamChieuDuLieu obj)
        {
            string sql = @" Update ThamChieuDuLieu
                            Set Cha = @Cha, 
                                Con = @Con, 
                                GiaTri = @GiaTri
                            Where Oid = @Oid";

            SqlParameter[] param = {
                                       new SqlParameter("@Oid", obj.Oid),
                                       new SqlParameter("@Cha", obj.Cha),
                                       new SqlParameter("@Con", obj.Con),
                                       new SqlParameter("@GiaTri", obj.GiaTri)
                                   };
            return DataProvider.Update(sql, param);
        }

        public int Delete(ThamChieuDuLieu obj)
        {
            string sql = @" Delete From ThamChieDuLieu Where Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", obj.Oid);
            return DataProvider.Update(sql, param);
        }


        public int Save(ThamChieuDuLieu obj)
        {
            throw new NotImplementedException();
        }
    }
}
